Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
ts-custom-error
Advanced tools
The ts-custom-error package is a TypeScript library that simplifies the creation of custom error classes. It provides a base class that can be extended to create custom error types with minimal boilerplate code.
Creating Custom Error Classes
This feature allows you to create custom error classes by extending the CustomError base class provided by ts-custom-error. The example demonstrates how to create a custom error class named MyCustomError and throw an instance of it.
class MyCustomError extends CustomError {
constructor(message: string) {
super(message);
this.name = 'MyCustomError';
}
}
try {
throw new MyCustomError('Something went wrong!');
} catch (error) {
console.error(error.name); // MyCustomError
console.error(error.message); // Something went wrong!
}
Preserving Stack Trace
This feature ensures that the stack trace is preserved when custom errors are thrown. The example shows how the stack trace includes the function where the error was thrown, which aids in debugging.
class AnotherCustomError extends CustomError {
constructor(message: string) {
super(message);
this.name = 'AnotherCustomError';
}
}
function problematicFunction() {
throw new AnotherCustomError('An error occurred in problematicFunction');
}
try {
problematicFunction();
} catch (error) {
console.error(error.stack); // Stack trace includes problematicFunction
}
The custom-error-generator package provides a way to create custom error classes with additional properties and methods. It is similar to ts-custom-error but offers more flexibility in defining custom error properties.
The extendable-error-class package allows for the creation of custom error classes with extended functionality. It is similar to ts-custom-error but focuses on providing a more comprehensive base class for error handling.
The error-subclass package is a lightweight library for creating custom error subclasses. It is similar to ts-custom-error but is more minimalistic and focuses on simplicity.
ts-custom-error
is a tiny (~500 bytes of minified & gzipped Javascript) package providing a CustomError
class and a customErrorFactory
function to easily extends native Error in node and evergreen browsers.
It's written in Typescript and try to offer the best development and debug experiences: bundled in Javascript with Typescript definition files, map files and bundled js files for various environments: transpiled to es5 with commonjs, module and umd exports, the umd bundle is also available minified for easy import in browsers.
Because extending native Error in node and in browsers is tricky
class MyError extends Error {
constructor(m) {
super(m)
}
}
doesn't work as expected in ES6 and is broken in Typescript.
CustomError
classSimply extends and call super
in you custom constructor.
import { CustomError } from 'ts-custom-error'
class HttpError extends CustomError {
public constructor(
public code: number,
message?: string,
) {
super(message)
}
}
...
new HttpError(404, 'Not found')
You may want more advanced contructor logic and custom methods, see examples
customErrorFactory
factoryCustom error contructor returned by the factory pass the same unit tests as Class constructor.
Factory still allows custom logic inside constructor:
import { customErrorFactory } from 'ts-custom-error'
const HttpError = customErrorFactory(function HttpError (code: number, message= '') {
this.code = code
this.message = message
})
...
new HttpError(404, 'Not found')
Custom Error from customErrorFactory
can:
HttpError(404, 'Not found')
import { customErrorFactory } from 'ts-custom-error'
const ValidationError = customErrorFactory(function ValidationError (message= 'Invalid parameter') {
this.message = message
}, TypeError)
Unexpected results are:
instanceof
You may fix this behaviour by:
--mangle 'except=["MyError"]'
(need to specify all custom error names) or --keep_fnames
/ --keep_classnames
(nothing to specify but your bundle size will be larger)import { CustomError } from 'ts-custom-error'
class MyError extends CustomError {
constructor() {
super()
// Set name explicitly as minification can mangle class names
Object.defineProperty(this, 'name', { value: 'MyError' })
}
}
import { customErrorFactory } from 'ts-custom-error'
const MyError = customErrorFactory(function MyError () {
// Set name explicitly as minification can remove function expression names
Object.defineProperty(this, 'name', { value: 'MyError' })
})
npm start
npm test
npm run coverage
npm run commit
⚠️ This project is mainly a pet project and its first purpose is to be a playground for various external services and tools:
Starting version 3.0.0 this project is under MIT licence, there are no code change between version 2.2.2 and version 3.0.0 but changing licence was considered as a breaking change. All versions < 3.0.0 are under WTFPL.
FAQs
Extend native Error to create custom errors
The npm package ts-custom-error receives a total of 72,494 weekly downloads. As such, ts-custom-error popularity was classified as popular.
We found that ts-custom-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.